﻿@model RecurringPaymentModel
@using Nop.Core.Domain.Catalog;
@Html.ValidationSummary(false)
@Html.HiddenFor(model => model.Id)

<div id="recurringpayment-edit">
    <ul>
        <li @Html.RenderSelectedTabIndex(0, GetSelectedTabIndex())>
            @T("Admin.RecurringPayments.Info")
        </li>
        <li @Html.RenderSelectedTabIndex(1, GetSelectedTabIndex())>
            @T("Admin.RecurringPayments.History")
        </li>
    </ul>
    <div>
        @TabInfo()
    </div>
    <div>
        @TabHistory()
    </div>
</div>
<script>
    $(document).ready(function () {
        $("#recurringpayment-edit").kendoTabStrip({
            animation: {
                open: {
                    effects: "fadeIn"
                }
            },
            select: tabstrip_on_tab_select
        });
    });
</script>

@{
    //custom tabs
    var eventMessage = new AdminTabStripCreated(this.Html, "recurringpayment-edit");
    EngineContext.Current.Resolve<IEventPublisher>().Publish(eventMessage);
    foreach (var eventBlock in eventMessage.BlocksToRender)
    {
        @eventBlock
    }
}

@*save selected tab index*@
<input type="hidden" id="selected-tab-index" name="selected-tab-index" value="@(GetSelectedTabIndex())">

@helper TabInfo()
    {
        
    <table class="adminContent">
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.InitialOrderId):
            </td>
            <td class="adminData">
                @Html.ActionLink(T("Admin.Common.View").Text, "Edit", "Order", new { id = Model.InitialOrderId }, new { })
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.CustomerEmail):
            </td>
            <td class="adminData">
                @Html.ActionLink(Model.CustomerEmail, "Edit", "Customer", new { id = Model.CustomerId }, new { })
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.CycleLength):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.CycleLength)
                @Html.ValidationMessageFor(model => model.CycleLength)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.CyclePeriodId):
            </td>
            <td class="adminData">
                @Html.DropDownListFor(model => model.CyclePeriodId, ((RecurringProductCyclePeriod)Model.CyclePeriodId).ToSelectList())
                @Html.ValidationMessageFor(model => model.CyclePeriodId)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.TotalCycles):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.TotalCycles)
                @Html.ValidationMessageFor(model => model.TotalCycles)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.CyclesRemaining):
            </td>
            <td class="adminData">
                @Model.CyclesRemaining
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.PaymentType):
            </td>
            <td class="adminData">
                @Model.PaymentType
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.StartDate):
            </td>
            <td class="adminData">
                @Model.StartDate
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.IsActive):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.IsActive)
                @Html.ValidationMessageFor(model => model.IsActive)
            </td>
        </tr>
    </table>
}
@helper TabHistory()
    { 
    <table class="adminContent">
        <tr>
            <td>
                @if (!String.IsNullOrEmpty(Model.NextPaymentDate))
                        {
                    @T("Admin.RecurringPayments.History.NextPaymentDate")
                    <text>:</text> @Model.NextPaymentDate
                    <input type="submit" name="processnextpayment" class="k-button" value="@T("Admin.RecurringPayments.History.ProcessNextPayment")" />
                        }
                @if (Model.CanCancelRecurringPayment)
                        {
                    <input type="submit" name="cancelpayment" class="k-button" value="@T("Admin.RecurringPayments.History.CancelPayment")" />
                        }
            </td>
        </tr>
    </table>
    <table class="adminContent">
        <tr>
            <td>
                <div id="history-grid"></div>

                <script>
                    $(document).ready(function () {
                        $("#history-grid").kendoGrid({
                            dataSource: {
                                type: "json",
                                transport: {
                                    read: {
                                        url: "@Html.Raw(Url.Action("HistoryList", "RecurringPayment", new { recurringPaymentId = Model.Id }))",
                                        type: "POST",
                                        dataType: "json"
                                    }
                                },
                                schema: {
                                    data: "Data",
                                    total: "Total",
                                    errors: "Errors"
                                },
                                error: function(e) {
                                    display_kendoui_grid_error(e);
                                    // Cancel the changes
                                    this.cancelChanges();
                                },
                                serverPaging: true,
                                serverFiltering: true,
                                serverSorting: true
                            },
                            pageable: {
                                refresh: true,
                                numeric: false,
                                previousNext: false,
                                info: false
                            },
                            editable: {
                                confirmation: false,
                                mode: "inline"
                            },
                            scrollable: false,
                            columns: [{
                                field: "OrderId",
                                title: "@T("Admin.RecurringPayments.History.Order")",
                                width: 200,
                                template: '<a href="@Url.Content("~/Admin/Order/Edit/")#=OrderId#">#=OrderId# - @T("Admin.Common.View")</a>'
                            }, {
                                field: "OrderStatus",
                                title: "@T("Admin.RecurringPayments.History.OrderStatus")",
                                width: 200
                            }, {
                                field: "PaymentStatus",
                                title: "@T("Admin.RecurringPayments.History.PaymentStatus")",
                                width: 200
                            }, {
                                field: "ShippingStatus",
                                title: "@T("Admin.RecurringPayments.History.ShippingStatus")",
                                width: 200
                            }, {
                                field: "CreatedOn",
                                title: "@T("Admin.RecurringPayments.History.CreatedOn")",
                                width: 200,
                                type: "date",
                                format: "{0:G}"
                            }]
                        });
                    });
                </script>
            </td>
        </tr>
    </table>
}